package com.demo.deal_service.service.impl;

import com.demo.deal_service.enums.ImageCutEnum;
import com.demo.deal_service.service.ImageService;
import com.demo.deal_service.util.ImgUtil;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

/**
 * 描述：通过调用工具类来对图片进行存取操作，获取图片地址
 * 创建者：lp
 */
@Service
public class ImageServiceImpl implements ImageService {
    @Override
    public String getGenImgUrl(Long id, String module, String suffix) {
        Optional<ImageCutEnum> imageCutEnum = ImageCutEnum
                .getImageCutEnumsByModule(module)
                .stream()
                .filter(e -> {
                    return e.getSuffix().equals(suffix);
                })
                .findAny();

        return ImgUtil.getGenImgPath(id,imageCutEnum.get());
    }

    @Override
    public String getSrcImgUrl(Long id) {
        return ImgUtil.getSourceImgPath(id);
    }

    @Override
    public void storeImage(Long id, InputStream inputStream, String module) throws IOException {
        ImgUtil.storeToLocal(id, inputStream, module);
    }
}
